package com.suncaper.shop.controller;

import com.suncaper.shop.domain.Product;
import com.suncaper.shop.service.DealerService;
import com.suncaper.shop.service.OrderService;
import com.suncaper.shop.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

@Controller
public class DealerController {

    @Autowired
    private ProductService productService;
    @Autowired
    private DealerService dealerService;

    @Autowired
    private OrderService orderService;

    @RequestMapping("/salerorder")
    public String salerorder(HttpSession session) {
        dealerService.getOrders(session);
        return "salerorder";
    }

    //商品入库功能
    @GetMapping("/edit/{pid}")
    public String setpro(@PathVariable("pid") Integer id, Model model) {
        Product product = productService.findByPid(id);
        model.addAttribute("theProduct", product);
        return "/edit";
    }

    @PutMapping("/edit")
    public String updateproduct(Product product) {
        product.setDeals(product.getMarketPrice() - product.getShopPrice());
        //System.out.println("保存的商品信息："+product.getSize());
        productService.saveByProduct(product);
        return "redirect:/account_saler";
    }

    //商品查看功能
    @GetMapping("/edit2/{pid}")
    public String lookPro(@PathVariable("pid") Integer id, Model model) {
        Product product = productService.findByPid(id);
        model.addAttribute("theProduct", product);
        return "/edit2";
    }

    //商品删除功能（未使用）
    @GetMapping("/delete/{pid}")
    public String delete(@PathVariable("pid") Integer id, Model model) {
        Product product = productService.findByPid(id);
        productService.delete(product);
        return "redirect:/account_saler";
    }


    //商品添加功能
    @PostMapping("/add2")
    public String addpro(Product product, Model model) {
        product.setDeals(product.getMarketPrice() - product.getShopPrice());
        if (!productService.saveproduct(product)) {
            model.addAttribute("msg", "相同的ID");
            return "add";
        }
        return "redirect:/account_saler";
    }

    @RequestMapping("/account_saler")
    public String account_saler(HttpSession session) {
        productService.findAllByuid(session);
        return "account_saler";
    }


    @PostMapping("/dealer/detail")
    @ResponseBody
    public Map<String, Object> showDetail(@RequestParam("oid") String oid,
                                          HttpSession session,
                                          HttpServletResponse response
    ) {
        Map<String, Object> showDetail = dealerService.showDetail(oid);
        System.out.println(showDetail.get("itemList"));
        return showDetail;
    }

    @RequestMapping("/dealer/update")
    public String updateState(@RequestParam("oid") String oid) {

        dealerService.updateState(oid, 2);
        return "redirect:/salerorder";
    }

    @RequestMapping("/dealer/get")
    public String updateGet(@RequestParam("oid") String oid) {

        dealerService.updateState(oid, 3);
        return "redirect:/order";
    }
}
